<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="导航|菜单名称">
          <a-input placeholder="请输入导航|菜单名称" v-decorator="['navName',validatorRules.navName]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="导航|菜单拼音|英文">
          <a-input placeholder="请输入导航|菜单拼音|英文" v-decorator="['navLetter',validatorRules.navLetter]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="图标名称">
          <a-input placeholder="请输入图标名称" v-decorator="['iconName',validatorRules.iconName]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="跳转URL">
          <a-input placeholder="请输入跳转URL" v-decorator="['h5Url',validatorRules.h5Url]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="小程序跳转路由">
          <a-input placeholder="请输入小程序跳转路由" v-decorator="['liteUrl',validatorRules.liteUrl]" />
        </a-form-item>
        <a-form-item label="导航|菜单图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-upload
            listType="picture-card"
            class="avatar-uploader"
            :showUploadList="false"
            :action="uploadAction"
            :data="{'isup':1}"
            :headers="headers"
            :beforeUpload="beforeUpload"
            @change="handleChange"
          >
            <img v-if="picUrl=='Has no pic url yet'" :src="getImgUrlView()" alt="图片文件"
                 style="height:104px;max-width:300px"/>
            <div v-else>
              <a-icon :type="uploadLoading ? 'loading' : 'plus'"/>
              <div class="ant-upload-text">上传</div>
            </div>
          </a-upload>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排序ID">
          <a-input-number placeholder="请输入排序数字" v-decorator="[ 'sortId', {}]" style="ime-mode:Disabled"  onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否显示">
          <a-radio-group v-decorator="['isShow',{'initialValue':0}]">
            <a-radio :value="0">显示</a-radio>
            <a-radio :value="1">隐藏</a-radio>
          </a-radio-group>
        </a-form-item>
		
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import Vue from 'vue'
  import {ACCESS_TOKEN} from "@/store/mutation-types"
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "LiteConfigNavModal",
    data () {
      return {
        title:"操作",
        picUrl: null,
        uploadLoading: false,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          navName: {rules: [{required: true, message: '请输入导航|菜单名称'}]},
          navLetter: {rules: [{required: false, message: '请输入导航|菜单拼音|英文'}]},
          iconName: {rules: [{required: false, message: '请输入图标名称'}]},
          h5Url: {rules: [{required: false, message: '请输入跳转URL'}]},
          liteUrl: {rules: [{required: false, message: '请输入小程序跳转路由'}]},
          imgPath: { rules: [{ required: true, message: '请选择图片' }] },
        },
        url: {
          ImgServer: window._CONFIG['staticDomainURL'],
          fileUpload: window._CONFIG['domianURL'] + "/sys/common/upload",
          add: "/config/liteConfigNav/add",
          edit: "/config/liteConfigNav/edit",
        },
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token": token}
    },
    computed: {
      uploadAction: function () {
        return this.url.fileUpload;
      }
    },
    methods: {
      beforeUpload: function (file) {
        var fileType = file.type;
        console.info("文件类型：" + fileType);
      },
      handleChange(info) {
        console.log('info', info)
        this.picUrl = "";
        if (info.file.status === 'uploading') {
          this.uploadLoading = true;
          return
        }
        if (info.file.status === 'done') {
          var response = info.file.response;
          this.uploadLoading = false;
          if (response.success) {
            this.model.imgPath = response.message;
            this.picUrl = "Has no pic url yet";
          } else {
            this.$message.warning(response.message);
          }
        }
      },
      getImgUrlView() {
        return this.url.ImgServer + "/" + this.model.imgPath;
      },
      add() {
        this.picUrl = null;
        this.edit({imgPath: ''});
      },
      edit (record) {
        if (record.imgPath != "") {
          this.picUrl = "Has no pic url yet";
        }
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'navName','navLetter','iconName','h5Url','liteUrl','sortId','isShow'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>
  .ant-input-number {
    font-family: "Chinese Quote", -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
    font-variant: tabular-nums;
    box-sizing: border-box;
    list-style: none;
    position: relative;
    padding: 4px 11px;
    width: 100%;
    height: 32px;
    font-size: 14px;
    line-height: 1.5;
    color: rgba(0, 0, 0, 0.65);
    background-color: #fff;
    background-image: none;
    transition: all 0.3s;
    margin: 0;
    padding: 0;
    display: inline-block;
    border: 1px solid #d9d9d9;
    border-radius: 4px;
  }
  .ant-input-number:hover {
    border-color: #40a9ff;
    border-right-width: 1px !important;
  }
  .ant-input-number-focused {
    border-color: color(#1890ff, 5);
    box-shadow: 0 0 0 2px rgba(64,169,255, 0.2);
    border-right-width: 1px !important;
  }
</style>